/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine.internal;

import com.googlecode.boringengine.DefaultFont;
import com.googlecode.boringengine.Font;
import com.googlecode.boringengine.GameEngine;
import com.googlecode.boringengine.GameLoader;
import com.googlecode.boringengine.Graphics;
import com.googlecode.boringengine.Log;
import com.googlecode.boringengine.Menu;
import com.googlecode.boringengine.Render;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class GameMenu extends Menu<GameLoaderInfo> implements GameEngine {

    private GameLoaderInfo loadFile = new GameLoaderInfo();
    private boolean inited;
    
    public GameMenu() {
        super(0, 40, 800, 560, 40);
    }
    public void init(GameLoader loader) {
        if (inited) return;
        File gameDir = new File("games");
        File[] files = gameDir.listFiles();
        for (File f : files) {
            GameLoader l;
            try {
                l = f.isDirectory() ? new GameLoaderDir(f) : new GameLoaderFile(f);
            } catch (IOException ex) {
                Log.warn("Exception loading %s: %s", f, ex);
                continue;
            }
            GameLoaderInfo i = new GameLoaderInfo();
            if (l.spriteIcon != null)
                i.icon = Graphics.loadSpriteFromScanner(new Scanner(l.getFile(l.spriteIcon)));
            i.name = l.name == null ? "NULL" : l.name;
            i.desc = l.desc == null ? "NULL" : l.desc;
            i.version = l.version == null ? "NULL" : l.version;
            i.l = l;
            i.f = f;
            addItem(i);
        }
        loadFile.icon = Graphics.loadSpriteFromFile("resources/open.sprite");
        loadFile.name = "Load Game";
        loadFile.desc = "Load a game not in the games directory";
        loadFile.version = "";
        addItem(loadFile);
        inited = true;
    }

    @Override
    public void draw() {
        Render.clearScreen();
        Font f = DefaultFont.getDefaultFont();
        Render.addScale(2);
        f.draw(400 - f.getWidth("Select Game"), 5, "Select Game");
        Render.addScale(.5);
        super.draw();
    }

    protected void draw(GameLoaderInfo info, int x, int y, boolean isSelected) {
        Render.moveOverWithoutScale(x, y);
        Font f = DefaultFont.getDefaultFont();
        if (info.icon != null)
            Graphics.drawSprite(info.icon, 10, 5);
        f.draw(50, 5, info.name + " " + info.version);
        f.draw(50, 21, info.desc);
        if (isSelected) {
            Graphics.drawRect(4, 1, 792, 2, 0);
            Graphics.drawRect(4, 38, 792, 2, 0);
            Graphics.drawRect(4, 1, 2, 38, 0);
            Graphics.drawRect(794, 1, 2, 38, 0);
        }
        Render.moveOverWithoutScale(-x, -y);
    }

    protected void menuItemClicked(GameLoaderInfo info) {
        boolean attemptedLoading = false;
        try {
            if (info == loadFile && !loadAFile())
                return;
            ClassLoader cl = URLClassLoader.newInstance(new URL[]
                    {info.f.toURI().toURL()});
            GameLoader l = info.l;
            GameEngine obj = (GameEngine) cl.loadClass(l.engineClass).newInstance();
            attemptedLoading = true;
            Engine.initGame(obj, l);
        } catch (Exception e) {
            Log.errorException(e, "Error loading game");
            if (attemptedLoading) Engine.initGame(this, new GameLoaderEngine());
        }
    }


    private boolean loadAFile() throws IOException {
        JFileChooser filez = new JFileChooser();
        filez.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int ret = filez.showOpenDialog(null);
        if (ret != JFileChooser.APPROVE_OPTION) return false;
        File f = filez.getSelectedFile();
        GameLoader l;
        if (f.isDirectory()) l = new GameLoaderDir(f);
        else l = new GameLoaderFile(f);
        loadFile.f = f;
        loadFile.l = l;
        return true;
    }

    public void quit() {}

}
